package cn.com.qtgl.sso.oidc.comp.service;

import cn.com.qtgl.sso.oidc.comp.config.SsoConfig;
import cn.com.qtgl.sso.oidc.comp.config.SsoScopesConfig;
import cn.com.qtgl.sso.oidc.model.config.FrontendSsoConfig;
import cn.com.qtgl.sso.oidc.model.config.SsoConfigBody;
import cn.com.qtgl.sso.oidc.model.config.SsoConfigMetadata;
import cn.melonlib.cores.commons.comp.utils.UriPathUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SsoConfigService {

    @Resource
    private SsoConfig ssoConfig;

    @Resource
    private SsoScopesConfig ssoScopesConfig;

    @Resource
    private UriPathUtils uriPathUtils;
    public FrontendSsoConfig getConfig(){
        FrontendSsoConfig frontendSsoConfig=new FrontendSsoConfig();
        SsoConfigMetadata ssoConfigMetadata=new SsoConfigMetadata();
        ssoConfigMetadata.setIssuer(uriPathUtils.getHttpAbsolutePath(ssoConfig.getOauthAuthorityUrl()));
        ssoConfigMetadata.setAuthorization_endpoint(ssoConfigMetadata.getIssuer()+"/authorize");
        ssoConfigMetadata.setUserinfo_endpoint(ssoConfigMetadata.getIssuer()+"/userinfo");
        ssoConfigMetadata.setEnd_session_endpoint(uriPathUtils.getHttpAbsolutePath("/sso/logout"));
        ssoConfigMetadata.setJwks_uri(ssoConfigMetadata.getIssuer()+"/jwks.json");
        SsoConfigBody ssoConfigBody=new SsoConfigBody();
        ssoConfigBody.setResponseType("id_token token");
        ssoConfigBody.setAuthority(ssoConfigMetadata.getIssuer());
        ssoConfigBody.setClientId(ssoConfig.getOauthClientId());
        ssoConfigBody.setScope(ssoScopesConfig.getImplicit());
        ssoConfigBody.setMetadata(ssoConfigMetadata);
        frontendSsoConfig.setConfig(ssoConfigBody);
        return frontendSsoConfig;

    }
}
